<?php

// KIỂM TRA TỒN TẠI PAGE HAY KHÔNG
if(isset($_GET["page"]))
{
	// ĐƯA 2 FILE VÀO TRANG & KHỞI TẠO CLASS
	
	require_once "paging_ajax.php";
	$paging = new paging_ajax();
	
	
	// ĐẶT CLASS CHO THÀNH PHẦN PHÂN TRANG THEO Ý MUỐN
	$paging->class_pagination = "pagination";
	$paging->class_active = "active";
	$paging->class_inactive = "inactive";
	$paging->class_go_button = "go_button";
	$paging->class_text_total = "total";
	$paging->class_txt_goto = "txt_go_button";

	// KHỞI TẠO SỐ PHẦN TỬ TRÊN TRANG
    $paging->per_page = 10; 	
    
    // LẤY GIÁ TRỊ PAGE THÔNG QUA PHƯƠNG THỨC POST
    $paging->page = $_GET["page"];
    // VIẾT CÂU TRUY VẤN & LẤY KẾT QUẢ TRẢ VỀ
    $paging->text_sql = "SELECT pl.place_id
								,pl.place_name
								,pl.address
								,dt.name district_name
								,pe.name province_name
								,case
									when lk.count_like is null or lk.count_like = '' then 0
									else lk.count_like
								end count_like
								,case
									when cm.count_comment is null or cm.count_comment = '' then 0
									else cm.count_comment
								end count_comment
								,case
									when rt.point_rating is null or rt.point_rating = '' then 0
									else rt.point_rating
								end point_rating								
								,case
									when rp.count_report is null or rp.count_report = '' then 0
									else rp.count_report
								end count_report
								,pl.url_avatar
								,pl.name_avatar
							FROM place as pl 
								LEFT JOIN  
									(select place_id, FLOOR(sum(point)/count(id)) as point_rating
									 from  rating 
									 group by place_id) as rt
									on rt.place_id = pl.place_id
								LEFT JOIN
									(select place_id, count(id) as count_like
									 from  `like` 
									 group by place_id) as lk
									on lk.place_id = pl.place_id
								LEFT JOIN
									(select place_id, count(comment_id) as count_comment
									 from  `comment` 
									 group by place_id) as cm
									on cm.place_id = pl.place_id
								LEFT JOIN
									(select place_id, count(id) as count_report
									 from  `report` 
									 group by place_id) as rp
									on rp.place_id = pl.place_id
								LEFT JOIN district dt
									ON pl.district_id = dt.id
								LEFT JOIN province pe
									ON dt.province_id = pe.id
							WHERE 	IFNULL(count_report,0) >= 1							
							ORDER BY count_report DESC
									,point_rating DESC
									,count_like DESC
									,count_comment DESC
									,pl.place_name DESC";
    $result_pag_data = $paging->GetResult();

    // BIẾN CHỨA KẾT QUẢ TRẢ VỀ
	$message = "";
	$message .="<table class='table table-hover table-striped'>";
            $message .="<thead>";
              $message .="<tr>";
                $message .="<th class='status-col'>Tên Quán</th>";
                $message .="<th>Tổng báo lỗi</th>";
                $message .="<th class='action-col'>&nbsp;</th>";
              $message .="</tr>";
            $message .="</thead>";
            $message .="<tbody id='reports'>";
	
	// DUYỆT MẢNG LẤY KẾT QUẢ
	while ($row = mysql_fetch_array($result_pag_data)) {
		$message .="<tr>";
		$message .="<td class='' ><a href='./place.php?place_id=".$row['place_id']."'>".$row['place_name']."</a></td>";
		$message .="<td class='' >".$row['count_report']."</td>";
		$message .="<td class='center-col'>";
				$message .="<i class='icon-remove'>X</i>";
			
		$message .="</td>";
		$message .="</tr>";
	}
	$message .="</tbody>";
    $message .="</table>";
	// ĐƯA KẾT QUẢ VÀO PHƯƠNG THỨC LOAD() TRONG LỚP PAGING_AJAX
	$paging->data = "<div class='data'><ul>" . $message . "</ul></div>"; // Content for Data    
	echo $paging->Load();  // KẾT QUẢ TRẢ VỀ
		
} 

